<?php
namespace Admin\Controller;
use User\Api\UserApi as UserApi;

/**
 * 后台Goods控制器
 * @author keg
 */
class GoodsController extends AdminController {

    /**
     * 后台Goods列表
     * @author keg
     */
    public function index(){
        $Goods = M('Goods');
        //查询条件
        $map['delete'] = 0;   //未删除
        $name = I('name', '');  //查询商品名称
        $fpid = I('fpid', 0);
        $spid = I('spid', 0);
        $tpid = I('tpid', 0);

        if($name){
            $map['name'] = array('like', '%'.$name.'%');
        }
        $fpid && $map['fpid'] = $fpid;
        $spid && $map['spid'] = $spid;
        $tpid && $map['tpid'] = $tpid;

        //选择分类
        $fcaty = M('GoodsCategory')->where(array('status=1 AND pid=0'))->order('sort desc, id desc')->select();


        //查询及分页
        $count = $Goods->where($map)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $Goods->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('fcaty', $fcaty);
        $this->meta_title = '商品列表';
        // echo 'test';
        $this->display();
    }

    
    /**
     * 后台添加Goods
     * @author keg
     */
    public function add(){

        //post提交新增操作
        if(IS_POST){
            // print_r($_POST); exit;
            $data['fpid'] = I('fpid', 0);
            $data['spid'] = I('spid', 0);
            $data['tpid'] = I('tpid', 0);
            $data['name'] = $contData['name'] = I('name', '');
            $data['num'] = I('num', '');
            $attribute = I('attribute', '');
            $data['attribute'] = implode(';', $attribute);
            $data['price'] = I('price', '0.00');
            $data['market_price'] = I('market_price', '0.00');
            $data['cost_price'] = I('cost_price', '0.00');
            $data['stock'] = I('stock', 0);
            $data['weight'] = I('weight', 0);
            $data['sales'] = I('sales', 0);
            $data['clicks'] = I('clicks', 0);
            $data['collects'] = I('collects',0);
            $data['freight'] = I('freight', 0);
            $data['status'] = I('status', 1);
            $data['checked'] = I('checked', 0);
            $data['isapp'] = I('isapp', 0);
            $issku = I('issku', 0);
            $data['issku'] = $issku;
            $pics = I('pic', '');
            if($pics){
                $data['pic'] = $pics[0];
                $contData['imgs'] = implode(';', $pics);
            }

            $time = NOW_TIME;
            $data['add_time'] = $data['show_time'] =  $data['update_time'] = $time;
            $contData['add_time'] = $contData['update_time'] = $time;
            

            $contData['keys'] = I('keys', '');
            $contData['description'] = I('description', '');
            $contData['content'] = I('content', '');
            // $contData['app_content'] = I('app_content', '');

            if(!$data['tpid']){
                $this->error('必须选择商品分类');
            }
            //商品名称必须填写
            if(!$data['name']){
                $this->error('商品名称不能为空');
            }

            //检测类目是否有规格
            $GoodsSpecs = D('GoodsSpecs');
            $Specslist = $GoodsSpecs->where('status=1 AND pid='.$data['tpid'])->order('sort desc')->select();
            if(!$Specslist){
                $hassku = 0;
            }


            //商品规格
            if($issku==1){
                //商品有SKU则处理SKU信息
                $saleSpecsInfo = array();
                $saleSepcsName = array();
                if($Specslist){
                    foreach($Specslist as $k=>$v){
                        $mark = 'pro_'.$v['id'];
                        $saleSpecs = I($mark);
                        //所有种类的规格必须选择
                        if(!is_array($saleSpecs) || empty($saleSpecs)){
                            $hassku = 0;
                            break;
                        }
                        $saleSpecsInfo[$v['id']] = $saleSpecs;
                        $saleSepcsName[$v['id']] = $v['name'];
                    }
                }
                //再次判断是否可以组成sku
                if($issku==1){
                    //sku组合
                    $itemsInfo = $this->parseSku($saleSpecsInfo);
                }
            }

            //新增Goods
            $Goods = M('goods');
            $GoodsContent = M('GoodsContent');
            if($gid = $Goods->add($data)){
                $contData['id'] = $gid;

                //新增内容goodsConteng
                if($GoodsContent->add($contData)){
                      //新增item表格数据
                      $itemData = array();
                      $itemData['gid'] = $gid;
                      $itemData['fpid'] = $data['fpid'];
                      $itemData['spid'] = $data['spid'];
                      $itemData['tpid'] = $data['tpid'];
                      
                      $itemData['name'] = $data['name'];
                      $itemData['attribute'] = $data['attribute'];
                      $itemData['status'] = $data['status'];
                      $itemData['checked'] = $data['checked'];
                      $itemData['isapp'] = $data['isapp'];
                      $itemData['sales'] = $data['sales'];
                      $itemData['clicks'] = $data['clicks'];
                      $itemData['collects'] = $data['collects'];
                      $itemData['freight'] = $data['freight'];
                      $itemData['weight'] = $data['weight'];
                      $itemData['pic'] = $data['pic'];

                      $itemData['add_time'] = $itemData['show_time'] =  $itemData['update_time'] = $time;

                      $Goodsitem = D('GoodsItems');

                      if($issku==1){
                          //规格数据组合
                          foreach($itemsInfo as $k=>$v){
                              //新增规格数据
                              $ip = $v['val'].';p';
                              $im = $v['val'].';m';
                              $ic = $v['val'].';c';
                              $iq = $v['val'].';q';
                              $imb = $v['val'].';mb';

                              $itemData['price'] = I($ip);
                              $itemData['market_price'] = I($im);
                              $itemData['cost_price'] =  I($ic);
                              $itemData['stock'] =  I($iq);
                              $itemData['num'] =  I($imb);
                              $itemData['specs'] =  $v['val'];
                              $itemData['specsold'] =  $v['vo'];
                              $itemData['specsnew'] =  $v['vn'];
                              $itemData['issku'] = 1;

                              //新怎items
                              if(!$Goodsitem->add($itemData)){  
                                  $this->error('规格数据新增失败！');
                              }
                          }
                    }else{
                          //组合数据
                          $itemData['price'] = $data['price'];
                          $itemData['market_price'] = $data['market_price'];
                          $itemData['cost_price'] =  $data['cost_price'];
                          $itemData['stock'] =  $data['stock'];
                          $itemData['num'] =  $data['num'];
                          $itemData['specs'] =  '';
                          $itemData['specsold'] =  '';
                          $itemData['specsnew'] =  '';
                          $itemData['issku'] = 0;
                          //新怎items
                          if(!$Goodsitem->add($itemData)){
                              $this->error('规格数据新增失败！');
                          }
                    }
                    $this->success('新增成功', U('Goods/index'));
                }else{
                    $this->error('新增失败');
                }

                //新增goodsItems

            }else{
                $this->error('新增失败');
            }
        }

        //获取分类列表
        $GoodsCategory = D('GoodsCategory');
        $map['status'] = 1;
        $map['pid'] = 0;
        $list = $GoodsCategory->where($map)->order('sort desc, id desc')->select();
        
        $pics =array(1,2,3,4,5);   //五张产品图片的空数组
        $this->assign('pics', $pics);
        $this->assign('list', $list);
        $this->meta_title = '发布商品';
        $this->display();
    }


    //修改商品
    public function edit(){
        if(IS_POST){
            $data['id'] = I('id', 0);
            $data['fpid'] = I('fpid', 0);
            $data['spid'] = I('spid', 0);
            $data['tpid'] = I('tpid', 0);
            $data['name'] = $contData['name'] = I('name', '');
            $data['num'] = I('num', '');
            $attribute = I('attribute', '');
            $data['attribute'] = implode(';', $attribute);
            $data['price'] = I('price', '0.00');
            $data['market_price'] = I('market_price', '0.00');
            $data['cost_price'] = I('cost_price', '0.00');
            $data['stock'] = I('stock', 0);
            $data['weight'] = I('weight', 0);
            $data['sales'] = I('sales', 0);
            $data['clicks'] = I('clicks', 0);
            $data['collects'] = I('collects',0);
            $data['freight'] = I('freight', 0);
            $data['status'] = I('status', 1);
            $data['checked'] = I('checked', 0);
            $data['isapp'] = I('isapp', 0);
            $issku = I('issku', 0);
            $data['issku'] = $issku;
            $pics = I('pic', '');
            if($pics){
                $data['pic'] = $pics[0];
                $contData['imgs'] = implode(';', $pics);
            }

            $time = NOW_TIME;
            $data['show_time'] =  $data['update_time'] = $time;
            $contData['add_time'] = $contData['update_time'] = $time;
            
            $contData['id'] = $data['id'];
            $contData['keys'] = I('keys', '');
            $contData['description'] = I('description', '');
            $contData['content'] = I('content', '');
            // $contData['app_content'] = I('app_content', '');

            //没有id
            !$data['id'] && $this->error('信息提交失败');
            //没有分类
            !$data['tpid'] && $this->error('必须选择商品分类');
            //商品名称必须填写
            !$data['name'] && $this->error('商品名称不能为空');

            //检测类目是否有规格
            $GoodsSpecs = D('GoodsSpecs');
            $Specslist = $GoodsSpecs->where('status=1 AND pid='.$data['tpid'])->order('sort desc')->select();

            //没有商品规格
            !$Specslist &&  $hassku = 0;


            //商品规格
            if($issku==1){
                //商品有SKU则处理SKU信息
                $saleSpecsInfo = array();
                $saleSepcsName = array();
                if($Specslist){
                    foreach($Specslist as $k=>$v){
                        $mark = 'pro_'.$v['id'];
                        $saleSpecs = I($mark);
                        //所有种类的规格必须选择
                        if(!is_array($saleSpecs) || empty($saleSpecs)){
                            $hassku = 0;
                            break;
                        }
                        $saleSpecsInfo[$v['id']] = $saleSpecs;
                        $saleSepcsName[$v['id']] = $v['name'];
                    }
                }
                //再次判断是否可以组成sku
                if($issku==1){
                    //sku组合
                    $itemsInfo = $this->parseSku($saleSpecsInfo);
                }
            }

            //修改Goods
            $Goods = M('goods');
            $GoodsContent = M('GoodsContent');
            if($Goods->save($data)){
                //修改内容goodsConteng
                if($GoodsContent->save($contData)){
                      //修改item表格数据
                      $itemData = array();
                      $itemData['gid'] = $data['id'];
                      $itemData['fpid'] = $data['fpid'];
                      $itemData['spid'] = $data['spid'];
                      $itemData['tpid'] = $data['tpid'];
                      
                      $itemData['name'] = $data['name'];
                      $itemData['attribute'] = $data['attribute'];
                      $itemData['status'] = $data['status'];
                      $itemData['checked'] = $data['checked'];
                      $itemData['isapp'] = $data['isapp'];
                      $itemData['sales'] = $data['sales'];
                      $itemData['clicks'] = $data['clicks'];
                      $itemData['collects'] = $data['collects'];
                      $itemData['freight'] = $data['freight'];
                      $itemData['weight'] = $data['weight'];
                      $itemData['pic'] = $data['pic'];

                      $itemData['show_time'] =  $itemData['update_time'] = $time;

                      $Goodsitem = D('GoodsItems');

                      if($issku==1){
                          //如果开启规格，则所有有关规格状态改为0
                          $infos = $Goodsitem->where('gid='.$data['id'])->select();
                          foreach($infos as $aa=>$bb){
                              $tmp = array(
                                  'status'=>0,
                                  'id'=>$bb['id']
                                );
                              //状态修改为0
                              $Goodsitem->save($tmp);
                          }
                          //规格数据组合
                          foreach($itemsInfo as $k=>$v){
                              //判断是否存在已有的记录
                              $itemInfo = $Goodsitem->where('gid='.$data['id'].' AND specs="' . $v['val'] . '"')->find();

                              //新增规格数据
                              $ip = $v['val'].';p';
                              $im = $v['val'].';m';
                              $ic = $v['val'].';c';
                              $iq = $v['val'].';q';
                              $imb = $v['val'].';mb';
                              $itemData['price'] = I($ip);
                              $itemData['market_price'] = I($im);
                              $itemData['cost_price'] =  I($ic);
                              $itemData['stock'] =  I($iq);
                              $itemData['num'] =  I($imb);
                              $itemData['specs'] =  $v['val'];
                              $itemData['specsold'] =  $v['vo'];
                              $itemData['specsnew'] =  $v['vn'];
                              $itemData['delete'] = 0;
                              $itemData['status'] = 1;
                              $itemData['issku'] = 1;

                              //检查数据是否存在
                              if($itemInfo){
                                  $itemData['id'] = $itemInfo['id'];
                                  if(!$Goodsitem->save($itemData)){
                                      $this->error('规格数据修改失败！');
                                  }
                              }else{
                                  unset($itemData['id']);
                                  $itemData['add_time'] = $time;
                                  if(!$Goodsitem->add($itemData)){
                                      $this->error('规格数据修改失败！');
                                  }
                              }
                          }
                      }else{
                          $infos = $Goodsitem->where('gid='.$data['id'])->select();
                          foreach($infos as $aa=>$bb){
                              $tmp = array(
                                  'status'=>0,
                                  'id'=>$bb['id']
                                );
                              $Goodsitem->save($tmp);
                          }

                          $itemInfo = $Goodsitem->where('gid=' . $data['id'] . ' AND issku=0')->order('id desc')->find();
                          //组合数据
                          $itemData['price'] = $data['price'];
                          $itemData['market_price'] = $data['market_price'];
                          $itemData['cost_price'] =  $data['cost_price'];
                          $itemData['stock'] =  $data['stock'];
                          $itemData['num'] =  $data['num'];
                          $itemData['specs'] =  '';
                          $itemData['specsold'] =  '';
                          $itemData['specsnew'] =  '';
                          $itemData['delete'] = 0;
                          $itemData['issku'] = 0;
                          $itemData['status'] = 1;
                          //新怎items
                          //修改items
                          if($itemInfo){
                              //有数据则修改
                              $itemData['id'] = $itemInfo['id'];
                              if(!$Goodsitem->save($itemData)){
                                  $this->error('规格数据修改失败！');
                              }
                          }else{
                              //无数据则新增
                              $itemData['add_time'] = $time;
                              if(!$Goodsitem->add($itemData)){
                                  $this->error('规格数据修改失败！');
                              }
                          }
                    }
                    $this->success('修改成功', U('Goods/index'));
                }else{
                    $this->error('修改失败');
                }

                //新增goodsItems

            }else{
                $this->error('修改失败');
            }
        }

        //获取ID
        $id = I('id',0,'int');
        if($id<1){
            $this->error('商品信息获取失败');
        }

        //获取商品信息
        $Goods = M('Goods');
        $Goodscontent = M('GoodsContent');
        $Goodsitem = M('GoodsItems');

        $goodsInfo = $Goods->find($id); //商品
        $goodsContent = $Goodscontent->find($id); //内容

        // print_r($goodsContent);  exit;

        //信息获取失败
        if(!$goodsInfo || !$goodsContent){
          $this->error('商品信息获取失败');
        }

        //商品图片处理
        if($goodsContent['imgs']){
            $imgs = explode(';', $goodsContent['imgs']);
        }
        // print_r($imgs);
        //获取规格信息
        $specs = $this->comSpecs($goodsInfo['tpid'], $goodsInfo['id']);
        // print_r($specs); exit;
        //获取分类属性
        $Attribute = M('GoodsAttribute');
        $attributeList = $Attribute->where('pid = ' . $goodsInfo['tpid'] . ' AND status=1 ')->order('sort desc, id desc')->select();

        //组合属性
        $attrs = $this->comAttribute($goodsInfo['attribute'], $attributeList);
        // print_r($attrs); exit;

        //获取商品分类信息
        $Category = D('GoodsCategory');
        $list[] = $Category->where(' status=1 AND id='.$goodsInfo['fpid'])->find();
        $list[] = $Category->where(' status=1 AND id='.$goodsInfo['spid'])->find();
        $list[] = $Category->where(' status=1 AND id='.$goodsInfo['tpid'])->find();

        // print_r($specs);

        $this->assign('list', $list);
        $this->assign('goodsInfo', $goodsInfo);
        $this->assign('goodsContent', $goodsContent);
        $this->assign('attrs', $attrs);
        $this->assign('specs', $specs);
        $this->assign('imgs', $imgs);
        $this->meta_title = '商品编辑';
        $this->display();
    }


    /**
     *  编辑商品属性组合
     *  @param string $goodsAttr 商品已选属性 
     *  @param array $cateAttr 商品所属分类所有属性
     */
    public function comAttribute($goodsAttr, $cateAttr){
        //解析选中的属性
        if($goodsAttr){
            $tmp1 = array_filter(explode(';', $goodsAttr));
            if($tmp1){
                foreach($tmp1 as $k=>$v){
                    $t1 = explode('-', $v);
                    $t2 = explode(':', $t1[1]);
                    $tmp2[] = array($t1[0], $t2[0], $t2[1]);
                }
            }
        }


        //解析栏目的属性，并组合属性
        if($cateAttr){
            foreach($cateAttr as $k=>$v){
                $cateAttr[$k]['list'] = array_filter(explode('@#@', $v['value']));
                if($cateAttr[$k]['list'] && $tmp2){
                    foreach ($cateAttr[$k]['list'] as $key => $value) {
                        foreach($tmp2 as $val){
                            if(($val[0] == $v['id']) && ($val[2] == $value )){
                                $cateAttr[$k]['flag'] = $key;
                            }
                        }
                    }
                }
            }
        }
        //返回数据
        return $cateAttr;
    }

     /**
     *  编辑商品是规格处理
     *  @param int $pid  分类id
     *  @param int $id  主产品id
     */
    public function comSpecs($pid, $id){
        $specstr = '';
        $ids = array();
        $tmp_val = array();
        $Specs = M('GoodsSpecs');
        $SpecsList = $Specs->where('pid='.$pid)->select();
        $kinds = count($SpecsList);


        $Goodsitem = D('GoodsItems');
        //商品规格处理
        $where['gid'] = $id;
        $where['issku'] = 1;
        $where['delete'] = 0;
        $where['status'] = 1;
        $itemlist = $Goodsitem->where($where)->select();
        if($itemlist){
            foreach($itemlist as $k=>$v){
                $tmp_val[$v['specs'].';p'] = $v['price'];
                $tmp_val[$v['specs'].';m'] = $v['market_price'];
                $tmp_val[$v['specs'].';c'] = $v['cost_price'];
                $tmp_val[$v['specs'].';q'] = $v['stock'];
                $tmp_val[$v['specs'].';mb'] = $v['num'];
                $tmp[$k] = explode(';', $v['specsnew']);
                if($tmp[$k]){
                    foreach($tmp[$k] as $key=>$value){
                        $tmp2 = explode(':', $value);
                        $tmp3[$tmp2[0]][$tmp2[1]] = $tmp2[3]; 
                    }
                }
            }
        }


         //规格组合
        if($SpecsList){
            foreach ($SpecsList as $k => $v) {
                $ids[] = $v['id'];
                $tmpsv = explode('@#@', $v['value']);
                $values = array_filter($tmpsv);
                if($values){
                    if($values){

                        $specstr .= "<label class='prop_name'>" . $v['title']  . "</label>";
                        $specstr .= "<input type='hidden' name='pn_".$v['id']."' value='".$v['title']."'>";
                        $specstr .= "<ul data-id='" . $v['id'] . "' data-name='" . $v['title'] . "' class='prop_values'>";
                        foreach($values as $key=>$val){
                            //判断是否被选中了
                            if($tmp3[$v['id']][$key]){
                                $select_class = "class='selected'";
                                $is_checked = "checked='true'";
                                $rea_val = $tmp3[$v['id']][$key];
                            }else{
                                $select_class = '';
                                $is_checked = '';
                                $rea_val = $val;
                            }

                            $specstr .= "<li " . $select_class . ">";
                            $specstr .= "<input " . $is_checked . " type='checkbox' data-name='" . $val . "' data-pname='" . $v['title'] . "' data-pid='" . $v['id'] . "' data-id='" . $key . "' id='p_" . $v['id'] . "_" . $key . "' value='". $v['id'] . ':' . $key . "' name='pro_" . $v['id'] . "[]'>";
                            $specstr .= "<label for='p_" . $v['id'] . "_" . $key . "'>" . $val . "</label>";
                            $specstr .= "<input type='text' value='" . $rea_val . "' class='inputtext2 len10 val_list'  data-name='" . $val . "' data-pname='" . $v['title'] . "' data-pid='" . $v['id'] . "' data-id='" . $key . "' name='pvn_" . $v['id'] . ":" . $key . "'>";
                            $specstr .= "<input type='hidden' value='" . $val . "' name='pvo_" . $v['id'] . ":" . $key . "'>";;

                            $specstr .= "</li>";
                        }
                        $specstr .= "</ul>";
                    }
                }
            }
        }
        $specs['com'] = $specstr;
        $specs['ids'] = json_encode($ids);
        $specs['tmp_val'] = json_encode($tmp_val);
        return $specs;
    }



    /**
     *  新增商品时解析SKU
     */
    public function parseSku($sku = array()){
        $allSkus = array();
        if(count($sku)>0){
            foreach($sku as $k=>$v){
                $allSkus = $this->createSku($allSkus, $v);
            }
        }

        return $allSkus;
        
    }


    /**
     *  新增商品时组合SKU  array('1:2;2:12')
     */
    public function createSku($arr = array(), $sku = array()){
        $tmp = array();
        if(count($arr)>0){
            foreach($arr as $k=>$v){
                foreach($sku as $key=>$val){
                    //分割规格id
                    $pid = explode(':', $val);
                    $key_p = 'pn_'.$pid[0];     //规格名称post过来的name
                    $key_vo = 'pvo_'.$val;      //规格原值post过来的name
                    $key_vn = 'pvn_'.$val;      //规格新值post过来的name

                    $val_p = I($key_p, '');
                    $val_vo = I($key_vo, '');
                    $val_vn = I($key_vn, '');

                    $tmp_val['val'] = $v['val'] . ';' . $val; 
                    $tmp_val['vo'] = $v['vo'] . ';' . $val . ':' . $val_p . ':' . $val_vo;
                    $tmp_val['vn'] = $v['vn'] . ';' . $val . ':' . $val_p . ':' . $val_vn;

                    $tmp[count($tmp)] = $tmp_val;
                }
            }
        }else{
            foreach($sku as $key=>$val){
                //分割规格id
                $pid = explode(':', $val);
                $key_p = 'pn_'.$pid[0];     //规格名称post过来的name
                $key_vo = 'pvo_'.$val;      //规格原值post过来的name
                $key_vn = 'pvn_'.$val;      //规格新值post过来的name

                $val_p = I($key_p, '');
                $val_vo = I($key_vo, '');
                $val_vn = I($key_vn, '');

                $tmp_val['val'] = $val;
                $tmp_val['vo'] = $val . ':' . $val_p . ':' . $val_vo;
                $tmp_val['vn'] = $val . ':' . $val_p . ':' . $val_vn;

                $tmp[count($tmp)] = $tmp_val;
    
            }
        }
        return $tmp;
    }


    /**
     * Ajax获取子分类
     * @author keg
     */
    public function getSubCategory(){
        $pid = I('pid', 0);
        $GoodsCategory = D('GoodsCategory');
        $map['status'] = 1;
        $map['pid'] = $pid;
        $list = $GoodsCategory->where($map)->order('sort desc, id desc')->select();
        if($list){
            $this->ajaxReturn(array('status'=>1, 'info'=>$list));
        }else{
            $this->ajaxReturn(array('status'=>0, 'info'=>'子分类为空'));
        }
    }

    /**
     * Ajax 获取分类属性
    */
    public function getAttribute(){
        $pid = I('pid', 0);
        $GoodsAttribute = D('GoodsAttribute');
        $map['status'] = 1;
        $map['pid'] = $pid;
        $list = $GoodsAttribute->where($map)->order('sort desc, id desc')->select();
        if($list){
            //属性字符串分割成数组
            foreach ($list as $key => $val) {
                $list[$key]['attr'] = explode('@#@', $val['value']);
            }

            $this->ajaxReturn(array('status'=>1, 'info'=>$list));
        }else{
            $this->ajaxReturn(array('status'=>0, 'info'=>'子分类为空'));
        }
    }

    /**
     *  Ajax获取分类规格
     */
    public function getSpecs(){
        $pid = I('pid', 0);
        !$pid && $this->ajaxReturn(array('status'=>0, 'info'=>'没有规格信息'));

        //获取规格
        $GoodsSpecs = M('GoodsSpecs');
        $map['pid'] = $pid;
        $map['status'] = 1;

        $list = $GoodsSpecs->where($map)->order()->select();
        //没有规格
        !$list && $this->ajaxReturn(array('status'=>0, 'info'=>'没有规格信息'));
        //有规格
        $specstr = '';
        foreach($list as $k=>$v){
            $ids[] = $v['id'];
            $tmp = explode('@#@', $v['value']);
            $Specs = array_filter($tmp);
            
            $specstr .= "<label class='prop_name'>" . $v['title']  . "</label>";
            $specstr .= "<input type='hidden' name='pn_".$v['id']."' value='".$v['title']."'>";
            $specstr .= "<ul data-id='" . $v['id'] . "' data-name='" . $v['title'] . "' class='prop_values'>";
            foreach($Specs as $key=>$val){
                $specstr .= "<li >";
                $specstr .= "<input type='checkbox' data-name='" . $val . "' data-pname='" . $v['title'] . "' data-pid='" . $v['id'] . "' data-id='" . $key . "' id='p_" . $v['id'] . "_" . $key . "' value='". $v['id'] . ':' . $key . "' name='pro_" . $v['id'] . "[]'>";
                $specstr .= "<label for='p_" . $v['id'] . "_" . $key . "'>" . $val . "</label>";
                $specstr .= "<input type='text' value='" . $val . "' class='inputtext2 len10 val_list'  data-name='" . $val . "' data-pname='" . $v['title'] . "' data-pid='" . $v['id'] . "' data-id='" . $key . "' name='pvn_" . $v['id'] . ":" . $key . "'>";
                $specstr .= "<input type='hidden' value='" . $val . "' name='pvo_" . $v['id'] . ":" . $key . "'>";;

                $specstr .= "</li>";
            }
            $specstr .= "</ul>";
        }
        $this->ajaxReturn(array('status'=>1, 'info'=>$specstr, 'ids'=>$ids));
    }


    //删除商品，都是做逻辑删除，只是修改delete为1
    public function  del(){
        $ids = I('id', 0);
        !$ids && $this->error('没有记录可以删除');

        //组合查询条件
        $id = array_unique((array)$ids);
        $map1['id'] = array('in', $id);
        $map2['gid'] = array('in', $id);

        $Goods = M('Goods');
        $GoodsItems = M('GoodsItems');
        //逻辑删除主商品
        if($Goods->where($map1)->setField('delete', 1)){
            if($GoodsItems->where($map2)->setField('delete', 1)){
                $this->success('删除成功', U('Goods/index'));
            }else{
                $this->error('删除失败2');
            }
        }else{
            $this->error('删除失败3');
        }
    }


    //审核
    public function checked(){
        $ids = I('id', 0);
        $checked = I('get.checked',0);

        //id验证
        !$ids && $this->error('请选择要操作的数据');
        $id = array_unique((array)$ids);
        $map1['id'] = array('in', $id);
        $map2['gid'] = array('in', $id);

        $Goods = M('Goods');
        $GoodsItems = M('GoodsItems');

        $data['checked'] = $checked;
        $data['checked_time'] = NOW_TIME;

        //操作
        if($Goods->where($map1)->save($data)){
            if($GoodsItems->where($map2)->save($data)){
                $this->success('操作成功', U('Goods/index'));
            }else{
                $this->error('操作失败');
            }
        }else{
            $this->error('操作失败');
        }
    }

}
